import { computed, Ref } from "vue";
import { message as Message } from "ant-design-vue";
import { shipmentBoxVolumeExcelListApi, passShipmentQuotationExcelkApi } from "@/api/waybillManagement/customerServiceWaybill";
import CommonHook from "@/hook/commonHook";
export default function ExportHook(state: {
  dialogOpts: Base.DialogOpts;
  tableRef: Ref<any>;
  activeKey: Ref<string>;
  getParams: () => Base.GeneraDataType;
  loading: Ref<boolean>;
  searchHandle: () => void;
}) {
  const { dialogOpts, tableRef, activeKey, getParams, loading, searchHandle } = state;
  const selectRows = computed<Array<ClientWaybillManagement.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  const { downloadFileByBlob } = CommonHook();
  // 使用模版导出
  const templateExportHandle = () => {
    dialogOpts.title = `模版导出`;
    dialogOpts.width = "520px";
    dialogOpts.component = "TemplateExport";
    dialogOpts.type = Number(activeKey.value);
    dialogOpts.passData = { searchForm: getParams(), selectRows: selectRows.value };
    dialogOpts.visible = true;
  };

  // 自定义模版导出
  const customExportHandle = () => {
    dialogOpts.title = `自定义模版导出`;
    dialogOpts.width = "800px";
    dialogOpts.component = "CustomExport";
    dialogOpts.type = Number(activeKey.value);
    dialogOpts.passData = { searchForm: getParams(), selectRows: selectRows.value };
    dialogOpts.visible = true;
  };

  // 发票导出
  const invoiceExportHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    const index = selectRows.value.findIndex((t: BusinessWaybill.ListRows) => t.makeInvoiceStatus === -1);
    if (index !== -1) return Message.error("只能勾选已制作发票的运单!");
    dialogOpts.title = "发票导出";
    dialogOpts.width = "520px";
    dialogOpts.passData = selectRows.value;
    dialogOpts.component = "InvoiceExport";
    dialogOpts.visible = true;
  };

  //材积明细导出
  const VolumeExportHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    loading.value = true;
    return shipmentBoxVolumeExcelListApi(selectRows.value.map((t: any) => t.id))
      .then((res: any) => {
        const { filename } = res.headers;
        const name = filename.split(".");
        name && name.length && downloadFileByBlob(res.data, `材积明细.${name[1]}`);
      })
      .finally(() => {
        loading.value = false;
      });
  };
  //导出报价
  const passShipmentQuotationExcelHandle = () => {
    loading.value = true;
    const params = {
      ...getParams(),
      idList: selectRows.value.length ? selectRows.value.map((t: any) => t.id) : undefined
    };
    return passShipmentQuotationExcelkApi(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          Message.success("操作成功,请前往导出中心导出!");
          searchHandle();
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  return {
    templateExportHandle,
    customExportHandle,
    invoiceExportHandle,
    VolumeExportHandle,
    passShipmentQuotationExcelHandle
  };
}
